package com.example.project.utils;

/**
 * 数字转换工具类
 * 
 * @author system
 * @date 2025-11-18
 */
public class NumberConvertUtil {

    /**
     * 阿拉伯数字转中文数字
     * 
     * @param arabicNumber 阿拉伯数字
     * @return 中文数字
     */
    public static String arabicToChinese(int arabicNumber) {
        if (arabicNumber == 0) {
            return "零";
        }
        
        String[] chineseUnits = {"", "十", "百", "千", "万", "十", "百", "千", "亿"};
        String[] chineseNumbers = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        
        String result = "";
        String numberStr = String.valueOf(arabicNumber);
        int length = numberStr.length();
        
        for (int i = 0; i < length; i++) {
            int digit = Character.getNumericValue(numberStr.charAt(i));
            int unitPos = length - i - 1;
            
            if (digit != 0) {
                result += chineseNumbers[digit] + chineseUnits[unitPos];
            } else {
                // 处理连续的零
                if (i < length - 1 && Character.getNumericValue(numberStr.charAt(i + 1)) != 0) {
                    result += chineseNumbers[0];
                }
            }
        }
        
        // 处理特殊情况：十开头
        if (result.startsWith("一十")) {
            result = result.substring(1);
        }
        
        return result;
    }

    /**
     * 中文数字转阿拉伯数字
     * 
     * @param chineseNumber 中文数字
     * @return 阿拉伯数字
     */
    public static int chineseToArabic(String chineseNumber) {
        if (chineseNumber == null || chineseNumber.trim().isEmpty()) {
            return 0;
        }
        
        String[] chineseNumbers = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String[] chineseUnits = {"十", "百", "千", "万", "亿"};
        int[] unitValues = {10, 100, 1000, 10000, 100000000};
        
        int result = 0;
        int temp = 0;
        
        for (int i = 0; i < chineseNumber.length(); i++) {
            char c = chineseNumber.charAt(i);
            
            // 检查是否是数字
            boolean isNumber = false;
            for (int j = 0; j < chineseNumbers.length; j++) {
                if (String.valueOf(c).equals(chineseNumbers[j])) {
                    temp = j;
                    isNumber = true;
                    break;
                }
            }
            
            // 检查是否是单位
            boolean isUnit = false;
            for (int j = 0; j < chineseUnits.length; j++) {
                if (String.valueOf(c).equals(chineseUnits[j])) {
                    if (temp == 0) {
                        temp = 1;
                    }
                    result += temp * unitValues[j];
                    temp = 0;
                    isUnit = true;
                    break;
                }
            }
            
            // 如果不是单位，继续累积
            if (!isUnit && i == chineseNumber.length() - 1) {
                result += temp;
            }
        }
        
        return result;
    }

    /**
     * 数字转罗马数字
     * 
     * @param number 数字(1-3999)
     * @return 罗马数字
     */
    public static String toRomanNumeral(int number) {
        if (number < 1 || number > 3999) {
            throw new IllegalArgumentException("数字必须在1-3999之间");
        }
        
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        
        StringBuilder result = new StringBuilder();
        
        for (int i = 0; i < values.length; i++) {
            while (number >= values[i]) {
                result.append(symbols[i]);
                number -= values[i];
            }
        }
        
        return result.toString();
    }

    /**
     * 罗马数字转阿拉伯数字
     * 
     * @param romanNumeral 罗马数字
     * @return 阿拉伯数字
     */
    public static int fromRomanNumeral(String romanNumeral) {
        if (romanNumeral == null || romanNumeral.trim().isEmpty()) {
            return 0;
        }
        
        romanNumeral = romanNumeral.toUpperCase();
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        
        int result = 0;
        int index = 0;
        
        while (index < romanNumeral.length()) {
            boolean found = false;
            
            // 先尝试匹配两位的符号
            if (index + 1 < romanNumeral.length()) {
                String twoChar = romanNumeral.substring(index, index + 2);
                for (int i = 0; i < symbols.length; i++) {
                    if (symbols[i].equals(twoChar)) {
                        result += values[i];
                        index += 2;
                        found = true;
                        break;
                    }
                }
            }
            
            // 如果没有匹配两位，尝试匹配一位
            if (!found) {
                String oneChar = romanNumeral.substring(index, index + 1);
                for (int i = 0; i < symbols.length; i++) {
                    if (symbols[i].equals(oneChar)) {
                        result += values[i];
                        index += 1;
                        found = true;
                        break;
                    }
                }
            }
            
            if (!found) {
                throw new IllegalArgumentException("无效的罗马数字: " + romanNumeral);
            }
        }
        
        return result;
    }

    /**
     * 数字转英文单词
     * 
     * @param number 数字(0-999999999)
     * @return 英文单词
     */
    public static String toEnglishWords(int number) {
        if (number == 0) {
            return "zero";
        }
        
        if (number < 0) {
            return "minus " + toEnglishWords(-number);
        }
        
        String result = "";
        
        if (number >= 1000000000) {
            result += toEnglishWords(number / 1000000000) + " billion ";
            number %= 1000000000;
        }
        
        if (number >= 1000000) {
            result += toEnglishWords(number / 1000000) + " million ";
            number %= 1000000;
        }
        
        if (number >= 1000) {
            result += toEnglishWords(number / 1000) + " thousand ";
            number %= 1000;
        }
        
        if (number >= 100) {
            result += toEnglishWords(number / 100) + " hundred ";
            number %= 100;
        }
        
        if (number > 0) {
            if (!result.isEmpty()) {
                result += "and ";
            }
            
            String[] units = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
                            "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", 
                            "eighteen", "nineteen"};
            String[] tens = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
            
            if (number < 20) {
                result += units[number];
            } else {
                result += tens[number / 10];
                if (number % 10 > 0) {
                    result += " " + units[number % 10];
                }
            }
        }
        
        return result.trim();
    }

    /**
     * 安全的字符串转整数
     * 
     * @param str 字符串
     * @param defaultValue 默认值
     * @return 整数
     */
    public static int safeParseInt(String str, int defaultValue) {
        if (str == null || str.trim().isEmpty()) {
            return defaultValue;
        }
        
        try {
            return Integer.parseInt(str.trim());
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    /**
     * 安全的字符串转长整数
     * 
     * @param str 字符串
     * @param defaultValue 默认值
     * @return 长整数
     */
    public static long safeParseLong(String str, long defaultValue) {
        if (str == null || str.trim().isEmpty()) {
            return defaultValue;
        }
        
        try {
            return Long.parseLong(str.trim());
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    /**
     * 安全的字符串转浮点数
     * 
     * @param str 字符串
     * @param defaultValue 默认值
     * @return 浮点数
     */
    public static double safeParseDouble(String str, double defaultValue) {
        if (str == null || str.trim().isEmpty()) {
            return defaultValue;
        }
        
        try {
            return Double.parseDouble(str.trim());
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    /**
     * 判断字符串是否为数字
     * 
     * @param str 字符串
     * @return 是否为数字
     */
    public static boolean isNumeric(String str) {
        if (str == null || str.trim().isEmpty()) {
            return false;
        }
        
        try {
            Double.parseDouble(str.trim());
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断字符串是否为整数
     * 
     * @param str 字符串
     * @return 是否为整数
     */
    public static boolean isInteger(String str) {
        if (str == null || str.trim().isEmpty()) {
            return false;
        }
        
        try {
            Integer.parseInt(str.trim());
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}